
	import { Vue, Component } from 'vue-property-decorator'
	import { State } from 'vuex-class'
	import PageContainer from '@/moduleC/components/layout/PageContainer.vue'
	import { UserInfo } from '@/moduleC/types/user'
	import store from '@/moduleC/store'
	type KeyboardRow = string[]

	@Component({
		components: {
			PageContainer
		}
	})
	export default class Payment extends Vue {

		private amount = ''
		private greenRatio = 10 // 积分比例
		private orangeRatio = 90 // 现金比例
		private keyboardRows = [
			['7', '8', '9', '-'],
			['4', '5', '6', '+'],
			['1', '2', '3', '确认'],
			['.', '0', '×', '']
		] as KeyboardRow[]

		get actualAmount() : string {
			if (!this.amount) return '0.00'
			const amount = Number(this.amount)
			return ((amount * this.orangeRatio) / 100).toFixed(2)
		}

		get pointsAmount() : string {
			if (!this.amount) return '0'
			const amount = Number(this.amount)
			return ((amount * this.greenRatio) / 100).toFixed(0)
		}
		get userInfo() : any {
			return store.state.user
		}

		handleKeyPress(key : string) {
			if (key === '删除') {
				this.amount = this.amount.slice(0, -1)
			} else if (key === '+' || key === '-') {
				return
			} else if (key === '×') {
				this.amount = ''
			} else if (key !== '') {
				// 处理小数点
				if (key === '.' && this.amount.includes('.')) return
				// 限制小数点后两位
				if (this.amount.includes('.') && this.amount.split('.')[1]?.length >= 2) return
				// 限制整数位数
				if (!this.amount.includes('.') && this.amount.length >= 6) return
				this.amount += key
			}
		}

		async submitPayment() {
			if (!this.amount) {
				uni.showToast({
					title: '请输入支付金额',
					icon: 'none'
				})
				return
			}

			const points = Number(this.pointsAmount)
			if (points > this.userInfo.integral) {
				uni.showToast({
					title: '积分余额不足',
					icon: 'none'
				})
				return
			}

			uni.showLoading({ title: '支付中' })
			try {
				// 模拟支付过程
				await new Promise(resolve => setTimeout(resolve, 1500))
				uni.showToast({
					title: '支付成功',
					icon: 'success'
				})
				setTimeout(() => {
					uni.navigateBack()
				}, 1500)
			} finally {
				uni.hideLoading()
			}
		}
	}